package Underworld_Survival;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

public class GameRegistry {
    // 三级注册表
    private static final Map<String, Supplier<Item>> ITEMS = new HashMap<>();//背包所有道具
    private static final Map<String, Supplier<Buff>> BUFFS = new HashMap<>();//buff列表
    private static final Map<String, Supplier<Skill>> SKILLS = new HashMap<>();//拥有技能
    // 初始化注册（游戏启动时调用）
    public static void initialize() {

        EffectValueRegistry.register("fixed", params ->
                new FixedValue((Float)params.get("value")));

        EffectValueRegistry.register("dynamic", params ->
                new DynamicValue(EffectValueRegistry.createCalculator((String)params.get("formula"))));

        // 注册Buff -----------------
        registerBuff("buff_poison", poison::new);
        registerBuff("buff_burning", burning::new);
        registerBuff("buff_boilingBlood", boilingBlood::new);
        registerBuff("buff_adamant", adamant::new);
        registerBuff("buff_indomitable", indomitable::new);
        registerBuff("buff_revitalize", revitalize::new);
        registerBuff("buff_curse", curse::new);
        registerBuff("buff_bloodthirsty", bloodthirsty::new);

        // 注册技能 -----------------
        registerSkill("skill_desperate_counterattack", desperate_counterattack::new);
        registerSkill("skill_normalAttack", normalAttack::new);
        registerSkill("skill_shadowlessKick", shadowlessKick::new);
        registerSkill("skill_overwhelmingSmash", overwhelmingSmash::new);
        registerSkill("skill_ironHeadSkill", ironHeadSkill::new);
        registerSkill("skill_eighteenDragon_SubduingPalms", eighteenDragon_SubduingPalms::new);
        registerSkill("skill_cycloneKick", cycloneKick::new);
        registerSkill("skill_poisonousClaw", poisonousClaw::new);
        registerSkill("skill_invincibleBodyDefense", invincibleBodyDefense::new);
        registerSkill("skill_agileSurge", agileSurge::new);
        registerSkill("skill_crystallizedCobweb", crystallizedCobweb::new);
        registerSkill("skill_summonCrystalCluster", summonCrystalCluster::new);
        registerSkill("skill_poisonFangCurse", poisonFangCurse::new);
        registerSkill("skill_afterimageSlash", afterimageSlash::new);
        registerSkill("skill_sword_QiRebound", sword_QiRebound::new);
        registerSkill("skill_lifeSiphon", lifeSiphon::new);
        registerSkill("skill_darkRecovery", darkRecovery::new);
        registerSkill("skill_magmaArmor", magmaArmor::new);
        registerSkill("skill_earth_SplittingSmash", earth_SplittingSmash::new);
        registerSkill("skill_doomsdayEruption", doomsdayEruption::new);

        registerSkill("skill_convergenceOfBlades", convergenceOfBlades::new);

        registerSkill("skill_endlessEdgeOfSpear", endlessEdgeOfSpear::new);

        // 注册通用物品 --------------
        registerItem("food_summerWatermelon", summerWatermelon::new);
        registerItem("food_largePieceOfMeat", largePieceOfMeat::new);
        registerItem("food_nutritiousGrass", nutritiousGrass::new);
        registerItem("food_essenceGrass", essenceGrass::new);
        registerItem("food_poisonousMushroom", poisonousMushroom::new);
        registerItem("food_strongLiquor", strongLiquor::new);

        registerItem("otherItem_elixir", elixir::new);
        registerItem("otherItem_goldenApple", goldenApple::new);
        registerItem("otherItem_resurrectionMedicine", resurrectionMedicine::new);
        registerItem("otherItem_panacea", panacea::new);

        registerItem("scroll_ironHeadSkill_SecretTechnique", ironHeadSkill_SecretTechnique::new);
        registerItem("scroll_cycloneKick_SecretTechnique", cycloneKick_SecretTechnique::new);
        registerItem("scroll_poisonousClaw_SecretTechnique", poisonousClaw_SecretTechnique::new);
        registerItem("scroll_invincibleBodyDefense_SecretTechnique", invincibleBodyDefense_SecretTechnique::new);
        registerItem("scroll_convergenceOfBlades_SecretTechnique", convergenceOfBlades_SecretTechnique::new);
        registerItem("scroll_healthScroll", healthScroll::new);

        registerItem("weapon_flyingSword", flyingSword::new);
        registerItem("weapon_heavyKnife", heavyKnife::new);
        registerItem("weapon_diamondSword", diamondSword::new);
        registerItem("weapon_flameSword", flameSword::new);

        registerItem("armor_woodenArmor", woodenArmor::new);
        registerItem("armor_chainMail", chainMail::new);
        registerItem("armor_thornyIron", thornyIron::new);
        registerItem("armor_crackedNail", crackedNail::new);

        registerItem("shoe_strawSandals", strawSandals::new);
        registerItem("shoe_sportsShoes", sportsShoes::new);
        registerItem("shoe_swiftShadowShoes", swiftShadowShoes::new);
        registerItem("shoe_magicalBoots", magicalBoots::new);

    }

    // 注册方法组
    public static void registerBuff(String id, Supplier<Buff> supplier) {
        BUFFS.put(id, supplier);
    }
    public static void registerSkill(String id, Supplier<Skill> supplier) {

        SKILLS.put(id, supplier);
    }
    public static void registerItem(String id, Supplier<Item> supplier) {
        ITEMS.put(id, supplier);
    }


    // 创建实例方法组
    public static Buff createBuff(String id) {
        return Optional.ofNullable(BUFFS.get(id))
                .map(Supplier::get)
                .orElseThrow(() -> new IllegalArgumentException("未知Buff: " + id));
    }
    public static Skill createSkill(String id) {
        return Optional.ofNullable(SKILLS.get(id))
                .map(Supplier::get)
                .orElseThrow(() -> new IllegalArgumentException("未知技能: " + id));
    }
    public static Item createItem(String id) {
        return Optional.ofNullable(ITEMS.get(id))
                .map(Supplier::get)
                .orElseThrow(() -> new IllegalArgumentException("未知物品: " + id));
    }

    // 效果值类型注册中心
    public static class EffectValueRegistry {
        private static final Map<String, EffectValueFactory> REGISTRY = new HashMap<>();

        // 注册效果值类型
        public static void register(String typeId, EffectValueFactory factory) {
            REGISTRY.put(typeId, factory);
        }

        // 创建效果值实例
        public static EffectValue create(String typeId, Map<String, Object> params) {
            return Optional.ofNullable(REGISTRY.get(typeId))
                    .map(f -> f.create(params))
                    .orElseThrow(() -> new IllegalArgumentException("未知效果类型: " + typeId));
        }

        // 初始化注册（游戏启动时调用）
        public static void initialize() {
            register("fixed", params ->
                    new FixedValue((Float)params.get("value")));

            register("dynamic", params ->
                    new DynamicValue(createCalculator((String)params.get("formula"))));
        }

        // 动态计算器工厂（示例）
        private static DynamicValue.EffectCalculator createCalculator(String formula) {
            return context -> {
                // 实际项目应使用公式解析引擎
                if (formula.equals("BASE_STR*0.2")) {
                    return context.getBaseStr() * 0.2f;
                }
                return 0;
            };
        }

        // 效果值工厂接口
        @FunctionalInterface
        public interface EffectValueFactory {
            EffectValue create(Map<String, Object> params);
        }
    }


}
